package ar.utils;

import ar.camera.pojo.GLPoint;

public class GLIconUtils {
	public static float calculateAngleBetweenOzAndEyeCam(GLPoint eye,
			GLPoint cam) {
		// (-1,0) //(cz-ez, cx-ex)
		float angle = 0f;
		float b = (float) Math.sqrt(Math.pow((cam.z - eye.z), 2)
				+ Math.pow((cam.x - eye.x), 2));
		angle = (float) Math.acos((-cam.z + eye.z) / b);
		if (cam.x > eye.x) {
			angle *= -1;
		}
		return angle;
		// in radian
	}

	/**
	 * 
	 * @param p
	 * @param center
	 * @param angle
	 *            in radian
	 * @return
	 */
	public static GLPoint rotateY(GLPoint p, GLPoint center, float angle) {
		GLPoint tmp = new GLPoint(p.x, p.y, p.z);
		// x' = x cos a -y sin a
		// y' = x sin a + y cos a
		float z = p.z - center.z;
		float x = p.x - center.x;

		tmp.z = (float) (z * Math.cos(angle) - x * Math.sin(angle)) + center.z;
		tmp.x = (float) (z * Math.sin(angle) + x * Math.cos(angle)) + center.x;

		return tmp;
	}

	/**
	 * 
	 * @param p
	 * @param center
	 * @param angle
	 *            in radian
	 * @return
	 */
	public static GLPoint rotateZ(GLPoint p, GLPoint center, float angle) {
		GLPoint tmp = new GLPoint(p.x, p.y, p.z);
		// x' = x cos a -y sin a
		// y' = x sin a + y cos a
		float x = p.x - center.x;
		float y = p.y - center.y;

		tmp.x = (float) (x * Math.cos(angle) - y * Math.sin(angle)) + center.x;
		tmp.y = (float) (x * Math.sin(angle) + y * Math.cos(angle)) + center.y;

		return tmp;
	}

	/**
	 * 
	 * @param p
	 * @param center
	 * @param angle
	 *            in radian
	 * @return
	 */
	public static GLPoint rotateX(GLPoint p, GLPoint center, float angle) {
		GLPoint tmp = new GLPoint(p.x, p.y, p.z);
		// y' = y cos a -Z sina
		// z' = Z sin a + Z cos a
		float y = p.y - center.y;
		float z = p.z - center.z;

		tmp.y = (float) (y * Math.cos(angle) - z * Math.sin(angle)) + center.y;
		tmp.z = (float) (y * Math.sin(angle) + z * Math.cos(angle)) + center.z;
		return tmp;
	}
}
